home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / utime.c < prev    next >
C/C++ Source or Header  |  1993-11-02  |  3KB  |  144 lines

  1. /* utime -- set the file modification time of the given file
  2.  * according to the time given; a time of 0 means the current
  3.  * time.
  4.  *
  5.  * stime -- set the current time to the value given.
  6.  *
  7.  * All times are in Unix format, i.e. seconds since to
  8.  * midnight, January 1, 1970 GMT
  9.  *
  10.  * written by Eric R. Smith, and placed in the public domain.
  11.  *
  12.  */
  13.  
  14. #include <compiler.h>
  15. #include <limits.h>
  16. #include <time.h>
  17. #include <errno.h>
  18. #include <osbind.h>
  19. #include <mintbind.h>
  20. #include <ioctl.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #ifdef __TURBOC__
  24. #include <sys\types.h>
  25. #else
  26. #include <sys/types.h>
  27. #endif
  28. #include "lib.h"
  29.  
  30. extern int __mint;
  31.  
  32. time_t _dostime __PROTO((time_t t));
  33.  
  34. /* convert a Unix time into a DOS time. The longword returned contains
  35.    the time word first, then the date word */
  36.  
  37. time_t
  38. _dostime(t)
  39.     time_t t;
  40. {
  41.         time_t time, date;
  42.     struct tm *ctm;
  43.  
  44.     if ((ctm = localtime(&t)) == NULL)
  45.         return 0;
  46.     time = (ctm->tm_hour << 11) | (ctm->tm_min << 5) | (ctm->tm_sec >> 1);
  47.     date = ((ctm->tm_year - 80) & 0x7f) << 9;
  48.     date |= ((ctm->tm_mon+1) << 5) | (ctm->tm_mday);
  49.     return (time << 16) | date;
  50. }
  51.  
  52. int
  53. utime(_filename, tset)
  54.       const char *_filename;
  55.       const struct utimbuf *tset;
  56. {
  57.     int fh;
  58.     unsigned long actime, modtime;
  59.     unsigned long dtime;    /* dos time equivalent */
  60.     
  61.     char filename[PATH_MAX];
  62.     struct _mutimbuf settime;
  63.     long res;
  64.  
  65.     if (tset)
  66.     {
  67.         modtime = _dostime (tset->modtime);
  68.         actime = _dostime (tset->actime);
  69.     }
  70.     else
  71.     {
  72.         actime = ((long) Tgettime () << 16) | (Tgetdate () & 0xFFFF);
  73.         modtime = actime;
  74.     }
  75.  
  76.     (void)_unx2dos(_filename, filename);
  77.  
  78.     settime.actime = (unsigned short) ((actime >> 16) & 0xFFFF);
  79.     settime.acdate = (unsigned short) (actime & 0xFFFF);
  80.     settime.modtime = (unsigned short) ((modtime >> 16) & 0xFFFF);
  81.     settime.moddate = (unsigned short) (modtime & 0xFFFF);
  82.     res = -EINVAL;
  83.     if (__mint > 92) {
  84.         if (tset)
  85.             res = Dcntl(FUTIME, (long) filename, (long) &settime);
  86.         else
  87.             res = Dcntl(FUTIME, (long) filename, (long) 0);
  88.     }
  89.     if (res != -EINVAL) {
  90.         if (res < 0) {
  91.             errno = (int) -res;
  92.             return -1;
  93.         }
  94.         return 0;
  95.     }
  96.     fh = (int) Fopen(filename, 2);
  97.     if (fh < 0) {
  98. #if 0
  99.         /* Kludge:  return success for dirs even though we failed */
  100.         if ((fh == -ENOENT) && (Fattrib(filename, 0, 0) == FA_DIR))
  101.             return 0;
  102. #endif
  103.         errno = -fh;
  104.         return -1;
  105.     }
  106.  
  107.     if (__mint > 90) {
  108.         if (tset)
  109.             res = Fcntl(fh, (long)&settime, FUTIME);
  110.         else
  111.             res = Fcntl(fh, (long)0, FUTIME);
  112.     }
  113.     if (res == -EINVAL)
  114.           {
  115.         dtime = modtime;
  116.         (void)Fdatime((_DOSTIME *) &dtime, fh, 1);
  117.           }
  118.  
  119.     if ((fh = Fclose(fh)) != 0) {
  120.         errno = -fh;
  121.         return -1;
  122.     }
  123.     return 0;
  124. }
  125.  
  126. int stime(t)
  127.     time_t *t;
  128. {
  129.     unsigned long dtime;
  130.     unsigned date, time;
  131.     long r;
  132.  
  133.     assert(t != 0);
  134.     dtime = _dostime(*t);
  135.     date = (int) (dtime & 0xffff);
  136.     time = (int) (dtime >> 16) & 0xffff;
  137.  
  138.     if (((r = Tsetdate(date)) != 0) || ((r = Tsettime(time)) != 0)) {
  139.         errno = r == -1 ? EBADARG : (int) -r;
  140.         return -1;
  141.     }
  142.     return 0;
  143. }
  144.